/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.merak.core;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.URL;

public final class Web {

	//~ Constructors /////////////////////////////////////////////////////////////
	//****************************************************************************
	protected Web() { }

	//~ Methods //////////////////////////////////////////////////////////////////
	//****************************************************************************
	public final InputStream post(String url,byte[] content) throws IOException {

		return this.post(url,content,null);

	}
	//****************************************************************************
	public InputStream post(String url,byte[] content,String name) throws IOException {

		// Open a connection to remote resource
		URL               addr = new URL(url);
		HttpURLConnection conn = (HttpURLConnection)addr.openConnection();

		// Set the content name
		if (name!=null) conn.setRequestProperty(
			"content-disposition", "attachment; filename=" + name
		);

		/* Configure this connection as a form. It will do a post to the
		 * remote server requesting the data */
		conn.setRequestProperty("Content-Type", "application/octet-stream");
		conn.setConnectTimeout(10000);
		conn.setDoOutput(true);

		// Set the request body
		if (content!=null) {
			conn.getOutputStream().write(content);
		}

		// Read the remote server's response
		return conn.getInputStream();

	}
	//****************************************************************************
	public InputStream post(String url,String query) throws IOException {

		// Open a connection to remote resource
		URL               addr = new URL(url);
		HttpURLConnection conn = (HttpURLConnection)addr.openConnection();

		/* Configure this connection as a form. It will do a post to the
		 * remote server requesting the data */
		conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
		conn.setConnectTimeout(10000);
		conn.setDoOutput(true);

		// Set the query string
		if (query!=null) {
			Writer writer = new OutputStreamWriter(conn.getOutputStream());
			writer.write(query);
			writer.close();
		}

		// Read the remote server's response
		return conn.getInputStream();

	}
	//****************************************************************************
	public StringBuffer downloadText(String url,String query) throws IOException {

		// Request remote data and prepare the local output file
		StringBuffer buffer = new StringBuffer();
		InputStream  input  = new BufferedInputStream(this.post(url,query));

		// Read the remote server's response
		for (int data=input.read(); data!=-1; data=input.read()) {
			buffer.append((char)data);
		}

		// return the text
		return buffer;

	}
	//****************************************************************************
	public File downloadFile(String url,String query)
		throws IOException
	{

		File outputFile = Application.io.createTempFile("download");
		this.downloadFile(url,query,outputFile);
		return outputFile;

	}
	//****************************************************************************
	public File downloadFile(String url,String query,File outputFile)
		throws IOException
	{
		// Auxiliary Variables
		byte[] buffer = new byte[2048];
		int    length = 0;

		// Request remote data and prepare the local output file
		InputStream  input  = new BufferedInputStream(this.post(url,query));
		OutputStream output = new BufferedOutputStream(new FileOutputStream(outputFile));

		// Read the remote server's response
	    while ( (length = input.read(buffer) ) >= 0) {
	    	output.write(buffer, 0, length);
	    }

		// return the file
		return outputFile;

	}
	//****************************************************************************

}
